package com.mikiya.plugin;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.mikiya.plugin.constant.PluginName;
import com.mikiya.plugin.dto.ClassInfoDTO;
import com.mikiya.plugin.dto.ClassMethodDTO;
import com.mikiya.plugin.util.ObjectUtil;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GlobalRecycleMainAction extends AnAction {
    private Project project = null;
    private ProjectParse projectParse = null;
    // 自定义渲染器
    private CopyableTreeCellRenderer renderer = new CopyableTreeCellRenderer(PluginName.GLOBAL_RECYCLE);
    private JPanel mainPanel = null;
    private JScrollPane scrollPane = new JScrollPane();
    private Content content = null;
    private JTextPane classTextPane = new JTextPane();

    @Override
    public void actionPerformed(AnActionEvent e) {
        project = e.getData(PlatformDataKeys.PROJECT);
        if (projectParse == null) {
            projectParse = new ProjectParse(PluginName.CYCLOMATIC_COMPLEXITY);
        }
        refreshData(false);
    }

    public void refreshData(boolean isDel) {
        projectParse.getAllFilesInProject(project, isDel);

        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("GlobalRecycleWindow");
        if (toolWindow == null) {
            return;
        }
        toolWindow.hide();
        DefaultMutableTreeNode threeNode = createTree();
        toolWindow.getContentManager().addContent(getThreeContent(threeNode, projectParse.getMsg()));
        // 无论当前状态为关闭/打开，进行强制打开ToolWindow
        toolWindow.show(new Runnable() {
            @Override
            public void run() {
            }
        });
    }

    public DefaultMutableTreeNode createTree() {
        String rootName = "root";
        if (project != null) {
            rootName = project.getName();
        }
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootName);
        if (ObjectUtil.isEmpty(projectParse.getClassList())) {
            return root;
        }

        for (ClassInfoDTO classInfo : projectParse.getClassList()) {
            DefaultMutableTreeNode twoRoot = new DefaultMutableTreeNode(classInfo);
            for (ClassMethodDTO method : classInfo.getMethodList()) {
                DefaultMutableTreeNode threeRoot = new DefaultMutableTreeNode(method);
                twoRoot.add(threeRoot);
            }
            root.add(twoRoot);
        }
        return root;
    }

    public Content getThreeContent(DefaultMutableTreeNode root, String msg) {
        if (mainPanel == null) {
            mainPanel = new JPanel(new FlowLayout());
            mainPanel.setSize(100, 400);
            JPanel toolPanel = new JPanel();

            JButton buttonAll = new JButton("全部", MyIcons.ALL);
            buttonAll.addActionListener(e -> refreshData(false));
            toolPanel.add(buttonAll);

            JButton buttonDel = new JButton("可疑", MyIcons.DEL);
            buttonDel.addActionListener(e -> refreshData(true));
            toolPanel.add(buttonDel);

            classTextPane.setText(msg);
            toolPanel.add(classTextPane);
            mainPanel.add(toolPanel);
        }

        classTextPane.setText(msg);

        //构造一个treeModel 对象，进行刷新树操作
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        treeModel.setRoot(root);
        JTree tree = new JTree(treeModel);
        // 设置渲染器
        tree.setCellRenderer(renderer);


        tree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                projectParse.clickEvent(project, renderer, e);
            }
        });

        //将tree添加道滚动条面板上
        scrollPane.setViewportView(tree);
        //将滚动条面板设置哼可见
        scrollPane.setVisible(true);
        //设置滚动条的滚动速度
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        //解决闪烁问题
        scrollPane.getVerticalScrollBar().setDoubleBuffered(true);
        mainPanel.add(scrollPane);

        mainPanel.setVisible(true);

        if (content == null) {
            content = ContentFactory.getInstance().createContent(mainPanel, "", false);
        } else {
            content.setComponent(mainPanel);
        }
        return content;
    }

}
